<?php
class VagasController extends AppController {

	var $name = 'Vagas';

	function index() {
		$this->Vaga->recursive = 0;
		$this->set('vagas', $this->paginate());
	}
	
	function adicionar(){
		App::import('Model','AreasVaga');
		App::import('Model','TipoEmpregosVaga');
		$AreasVaga = new AreasVaga();
		$TipoEmpregosVaga = new TipoEmpregosVaga();
		if(!empty($this->data)){
			$this->Vaga->create();
			if($this->Vaga->save($this->data)){
				$vaga_id = $this->Vaga->getID();
				$areasvaga = $this->data['AreasVaga']['area_id'];
				foreach($areasvaga as $k=>$v){
					$areavaga = array('AreasVaga'=>array('area_id'=>$v,'vaga_id'=>$vaga_id));
					$AreasVaga->create();
					$AreasVaga->save($areavaga);
				}
				$tipoempregosvaga = $this->data['TipoEmpregosVaga']['tipo_emprego_id'];
				foreach($tipoempregosvaga as $k=>$v){
					$tipoempregosvaga = array('TipoEmpregosVaga'=>array('tipo_emprego_id'=>$v,'vaga_id'=>$vaga_id));
					$TipoEmpregosVaga->create();
					$TipoEmpregosVaga->save($tipoempregosvaga);
				}
				$this->Session->setFlash(__('The vaga has been saved', true));
				$this->redirect(array('controller'=>'empresas','action' => 'empresa'));
				
			}
		}
		
		$areas  = $this->Vaga->Area->find('list');
		$tipo_empregos = $this->Vaga->TipoEmprego->find('list');
		$tempo_experiencias = $this->Vaga->TempoExperiencia->find('list');
		$paises = $this->Vaga->Paise->find('list');
		$provincias = $this->Vaga->Provincia->find('list');
		$distritos = $this->Vaga->Distrito->find('list',array('conditions'=>array('Distrito.provincia_id'=>5)));
		$this->set(compact('areas','tipo_empregos','tempo_experiencias','paises','provincias','distritos'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid vaga', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('vaga', $this->Vaga->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Vaga->create();
			if ($this->Vaga->save($this->data)) {
				$this->Session->setFlash(__('The vaga has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The vaga could not be saved. Please, try again.', true));
			}
		}
		$empresas = $this->Vaga->Empresa->find('list');
		$paises = $this->Vaga->Paise->find('list');
		$provincias = $this->Vaga->Provincium->find('list');
		$distritos = $this->Vaga->Distrito->find('list');
		$tempoExperiencias = $this->Vaga->TempoExperiencium->find('list');
		$areas = $this->Vaga->Area->find('list');
		$tipoEmpregos = $this->Vaga->TipoEmprego->find('list');
		$this->set(compact('empresas', 'paises', 'provincias', 'distritos', 'tempoExperiencias', 'areas', 'tipoEmpregos'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid vaga', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Vaga->save($this->data)) {
				$this->Session->setFlash(__('The vaga has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The vaga could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Vaga->read(null, $id);
		}
		$empresas = $this->Vaga->Empresa->find('list');
		$paises = $this->Vaga->Paise->find('list');
		$provincias = $this->Vaga->Provincium->find('list');
		$distritos = $this->Vaga->Distrito->find('list');
		$tempoExperiencias = $this->Vaga->TempoExperiencium->find('list');
		$areas = $this->Vaga->Area->find('list');
		$tipoEmpregos = $this->Vaga->TipoEmprego->find('list');
		$this->set(compact('empresas', 'paises', 'provincias', 'distritos', 'tempoExperiencias', 'areas', 'tipoEmpregos'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for vaga', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Vaga->delete($id)) {
			$this->Session->setFlash(__('Vaga deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Vaga was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
